#include <QtGui>

#include "imagewidget.h"
#include "linearfilter.h"
#include "medianfilter.h"
#include "window.h"

Window::Window()
{
    imageWidget = new ImageWidget;

    sizeSpinBox = new QSpinBox;
    sizeSpinBox->setRange(1, 32);
    sizeSpinBox->setSingleStep(2);
    sizeSpinBox->setValue(3);
    sizeSpinBox->setKeyboardTracking(false);
    sizeSpinBox->setAccelerated (false);

    sizeLabel = new QLabel(tr("Filter &Size:"));
    sizeLabel->setBuddy(sizeSpinBox);
    
    filterComboBox = new QComboBox;
    filterComboBox->addItem(tr("None"));
    filterComboBox->addItem(tr("Mean filter (linear)"));
    filterComboBox->addItem(tr("Median filter (nonlinear)"));

    filterLabel = new QLabel(tr("&Filter"));
    filterLabel->setBuddy(filterComboBox);

    openButton = new QPushButton(tr("&Open"));
    quitButton = new QPushButton(tr("&Quit"));

    connect(sizeSpinBox, SIGNAL(valueChanged(int)),
            this, SLOT(onChange()));
    connect(filterComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(onChange()));
    connect(openButton, SIGNAL(clicked()),
            this, SLOT(open()));
    connect(quitButton, SIGNAL(clicked()),
            qApp, SLOT(quit()));

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->setColumnStretch(3, 10);
    //    mainLayout->setHorizontalSpacing(20);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);
    mainLayout->addWidget(imageWidget, 0, 0, 1, 6);
    mainLayout->addWidget(sizeLabel, 1, 0);
    mainLayout->addWidget(sizeSpinBox, 1, 1);
    mainLayout->addWidget(filterLabel, 1, 2);
    mainLayout->addWidget(filterComboBox, 1, 3);
    mainLayout->addWidget(openButton, 1, 4);
    mainLayout->addWidget(quitButton, 1, 5);
    setLayout(mainLayout);

    setWindowTitle(tr("Filters"));

    currentFilter = 0;
}

Window::~Window()
{
    if (currentFilter)
        delete currentFilter;
}

void Window::onChange()
{
    //QApplication::setOverrideCursor(Qt::WaitCursor);
    enableWidgets(false);

    if (currentFilter) {
        delete currentFilter;
        currentFilter = 0;
    }

    QProgressDialog progressDialog(tr("Applying Filter..."), tr("&Cancel"), 0, originalImage.height()-1, this);
    progressDialog.setMinimumDuration(0);
    progressDialog.setWindowModality(Qt::ApplicationModal);

    switch(filterComboBox->currentIndex()) {
    case 2:
        currentFilter = new MedianFilter(sizeSpinBox->value(), &progressDialog);
        break;
    case 1:
        currentFilter = new LinearFilter(sizeSpinBox->value(), &progressDialog);
        break;
    }

    imageWidget->setImage(currentFilter ? currentFilter->filter(originalImage) : originalImage);

    enableWidgets(true);
    //QApplication::setOverrideCursor(Qt::ArrowCursor);
}

void Window::open()
{
    if (imageWidget->open())
	originalImage = imageWidget->image();
    filterComboBox->setCurrentIndex(0);
    sizeSpinBox->setValue(3);
}

void Window::enableWidgets(bool on)
{
    sizeLabel->setEnabled(on);
    sizeSpinBox->setEnabled(on);
    filterLabel->setEnabled(on);
    filterComboBox->setEnabled(on);
    sizeSpinBox->repaint();
}
